package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class BounceBall extends JPanel{
	private double t = 0;
	private double y= 0;
	private double shadowSize;
	private double grey;
	
	private double width;
	private double height;
	private double correctedY;
	
	public BounceBall()
	{
		setSize(400,400);
	}
	
	public void start() throws InterruptedException
	{
		while(true)
		{
			y = -0.02*t*t + 4.5*t;
			grey = 0.1 * y + 200;
			shadowSize = 0.2 * y + 50;
			width = -0.1 * y + 70;
			height = 0.1 * y + 50;
			correctedY = 250 - y;
			
			repaint();
		    Thread.sleep(40);
		    
			if (y < 0) {
		        t = 0;
		    }
		    t += 5;
		}
	}
	
	@Override
	public void paint(Graphics g) {
		Font font = new Font("Serif",Font.BOLD,40);
		g.setFont(font);
		
		Color backcolor = new Color(186, 255, 232);
		g.setColor(backcolor);
		g.fillRect(0, 0, 400, 400);
		g.setColor(Color.WHITE);
		g.fillRect(0, 250, 400,150);
		
		Color ballColor = new Color(71, 117, 255);
		g.setColor(ballColor);
		g.fillOval(200, (int)correctedY, (int)width, (int)height);
		
		g.setColor(Color.WHITE);
		g.drawString("N", 210, ((int)correctedY)+40);
		
		Color shadow = new Color((int)grey, (int)grey, (int)grey);
		g.setColor(shadow);
		g.fillOval(200, 300, (int)shadowSize, 10);
		
		
		
		
		//---------------------
		g.setColor(ballColor);
		g.fillOval(100, (int)correctedY, (int)width, (int)height);
		g.setColor(Color.WHITE);
		g.drawString("U", 110, ((int)correctedY)+40);
		
		g.setColor(shadow);
		g.fillOval(100, 300, (int)shadowSize, 10);
		//----------------
		g.setColor(ballColor);
		g.fillOval(300, (int)correctedY, (int)width, (int)height);
		
		g.setColor(Color.WHITE);
		g.drawString("O", 310, ((int)correctedY)+40);
		
		g.setColor(shadow);
		g.fillOval(300, 300, (int)shadowSize, 10);
	}
	
	public static void main(String[] args)
	{
		BounceBall bb = new BounceBall();
		JFrame jf = new JFrame();
		jf.setSize(400, 400);
		jf.add(bb);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		
		try {
			bb.start();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
